//
//  ToolsCell.m
//  PropertyMananger
//
//  Created by bh on 16/12/23.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "ToolsMenuCell.h"

#define TAG 200
@interface ToolsMenuCell ()<UIScrollViewDelegate>
{
    UIView *_backView1;
    UIView *_backView2;
    UIPageControl *_pageControl;
}
@end

@implementation ToolsMenuCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+ (instancetype)cellWithTableView:(UITableView *)tableView menuArray:(NSArray *)menuArray
{
  static NSString * cellID = @"ToolsCellID";
    ToolsMenuCell * toolCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!toolCell) {
        
        toolCell = [[ToolsMenuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID menuArray:menuArray];
        
    }
    toolCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return toolCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier menuArray:(NSArray *)menuArray
{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _backView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, 168)];
        _backView2 = [[UIView alloc]initWithFrame:CGRectMake(BHscreenW, 0, BHscreenW, 168)];
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, 180)];
        //
        scrollView.contentSize = CGSizeMake(BHscreenW*2, 180);
        //
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        //
        scrollView.showsHorizontalScrollIndicator = NO;
        [scrollView addSubview:_backView1];
        [scrollView addSubview:_backView2];
        [self addSubview:scrollView];
        
        //
        for(int i = 0; i < 16; i++) {
            if(i < 4) {
                
                CGRect frame = CGRectMake(i*BHscreenW/4, 0, BHscreenW/4, 84);
                NSString *title = [menuArray[i] objectForKey:@"title"];
                NSString *imagestr = [menuArray[i] objectForKey:@"image"];
                MenuBtnView *btnView = [[MenuBtnView alloc]initWithFrame:frame title:title imagestr:imagestr];
                btnView.tag = TAG + i;
                [_backView1 addSubview:btnView];
                
                //划线
//                UIView * rightLine = [[UIView alloc]init];
//                rightLine.frame = CGRectMake(i*BHscreenW/4, 0, 1, 84);
//                [_backView1 addSubview:rightLine];
//                rightLine.backgroundColor = BHGrayColor;
//                //
//                UIView * bottomLine = [[UIView alloc]init];
//                bottomLine.frame = CGRectMake(i*BHscreenW/4,84, 103, 1);
//                bottomLine.backgroundColor = BHGrayColor;
//                [_backView1 addSubview:bottomLine];
                
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Clicktap:)];
                [btnView addGestureRecognizer:tap];
                
            }
            else if (i < 8) {
                
                CGRect frame = CGRectMake((i-4)*BHscreenW/4, 84, BHscreenW/4, 84);
                NSString *title = [menuArray[i] objectForKey:@"title"];
                NSString *imagestr = [menuArray[i] objectForKey:@"image"];
                MenuBtnView *btnView = [[MenuBtnView alloc]initWithFrame:frame title:title imagestr:imagestr];
                btnView.tag = TAG + i;
                [_backView1 addSubview:btnView];
                
                //划线
//                UIView * rightLine = [[UIView alloc]init];
//                rightLine.frame = CGRectMake((i-4)*BHscreenW/4, 84, 1, 84);
//                [_backView1 addSubview:rightLine];
//                rightLine.backgroundColor = BHGrayColor;
                
//                UIView * bottomLine = [[UIView alloc]init];
//                bottomLine.frame = CGRectMake((i-4)*BHscreenW/4,168, 105, 1);
//                bottomLine.backgroundColor = BHGrayColor;
//                [_backView1 addSubview:bottomLine];
                
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Clicktap:)];
                [btnView addGestureRecognizer:tap];
                
            }else if (i < 12) {
                CGRect frame = CGRectMake((i-8)*BHscreenW/4, 0, BHscreenW/4, 84);
                NSString *title = [menuArray[i] objectForKey:@"title"];
                NSString *imagestr = [menuArray[i] objectForKey:@"image"];
                MenuBtnView *btnView = [[MenuBtnView alloc]initWithFrame:frame title:title imagestr:imagestr];
                btnView.tag = TAG + i;
                [_backView2 addSubview:btnView];
                
                //划线
//                UIView * rightLine = [[UIView alloc]init];
//                rightLine.frame = CGRectMake((i-8)*BHscreenW/4, 0, 1, 84);
//                [_backView2 addSubview:rightLine];
//                rightLine.backgroundColor = BHGrayColor;
//                //
//                UIView * bottomLine = [[UIView alloc]init];
//                bottomLine.frame = CGRectMake((i-8)*BHscreenW/4,84, 103, 1);
//                bottomLine.backgroundColor = BHGrayColor;
//                [_backView2 addSubview:bottomLine];
                
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Clicktap:)];
                [btnView addGestureRecognizer:tap];
            } else {
                
                CGRect frame = CGRectMake((i-12)*BHscreenW/4, 84, BHscreenW/4, 84);
                NSString *title = [menuArray[i] objectForKey:@"title"];
                NSString *imagestr = [menuArray[i] objectForKey:@"image"];
                MenuBtnView *btnView = [[MenuBtnView alloc]initWithFrame:frame title:title imagestr:imagestr];
                btnView.tag = TAG + i;
                [_backView2 addSubview:btnView];
                
                //划线
//                UIView * rightLine = [[UIView alloc]init];
//                rightLine.frame = CGRectMake((i-12)*BHscreenW/4, 84, 1, 84);
//                [_backView2 addSubview:rightLine];
//                rightLine.backgroundColor = BHGrayColor;
                //
//                UIView * bottomLine = [[UIView alloc]init];
//                bottomLine.frame = CGRectMake((i-12)*BHscreenW/4,168, 103, 1);
//                bottomLine.backgroundColor = BHGrayColor;
//                [_backView2 addSubview:bottomLine];
                
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Clicktap:)];
                [btnView addGestureRecognizer:tap];
            }
        }
        
        int nuberPages = 2;
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((BHscreenW-20*nuberPages)/2, 160, 20*nuberPages, 20)];
        _pageControl.centerX = self.contentView.centerX;
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = nuberPages;
        _pageControl.hidesForSinglePage = YES;

        [self.contentView addSubview:_pageControl];
        [_pageControl setCurrentPageIndicatorTintColor:Theme_Color];
        [_pageControl setPageIndicatorTintColor:[UIColor grayColor]];
    }
    return self;
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat scrollViewW = scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int page = (x + scrollViewW/2)/scrollViewW;
    _pageControl.currentPage = page;
}
#pragma mark - 点击
-(void)Clicktap:(UITapGestureRecognizer *)sender{
    
    NSLog(@"tag:%ld",sender.view.tag);
    if (self.block != nil) {
        self.block(sender.view.tag);
    }
   
    
}


@end
